package com.hengfoxwolf.ffmpegdemo.util
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioTrack
import android.view.Surface

class VideoPlayer {

    external fun init()

    external fun play(path:String,surface: Surface)

    external fun playPause(playPause: Boolean)

    external fun destroy()

    fun createAudioTrack(sampleRateInHz: Int, nb_channels: Int): AudioTrack {
        val channelConfig = when (nb_channels) {
            1 -> {
                AudioFormat.CHANNEL_OUT_MONO
            }
            2 -> {
                AudioFormat.CHANNEL_OUT_STEREO
            }
            else -> {
                AudioFormat.CHANNEL_OUT_MONO
            }
        }
        val bufferSize = AudioTrack.getMinBufferSize(
            sampleRateInHz,
            channelConfig,
            AudioFormat.ENCODING_PCM_16BIT
        )

        /*
        * int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
            int bufferSizeInBytes, int mode
        * */
        return AudioTrack(
            AudioManager.STREAM_MUSIC,
            sampleRateInHz,
            channelConfig,
            AudioFormat.ENCODING_PCM_16BIT,
            bufferSize,
            AudioTrack.MODE_STREAM
        )
    }

    companion object {
        init {
            System.loadLibrary("video-player-lib")
        }
    }

}